import { incomeDetails, orderTypeList } from '../../api/index'
import { getYMDHMS } from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    kssj: '',
    jssj: '',
    showDate: false,
    sjpd: '',
    currentDate: new Date(new Date().toLocaleDateString()).getTime(),
    currentDateb: new Date(new Date().toLocaleDateString()).getTime(),
    minDate: new Date('2000/01/01').getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    checkAll: false,
    columns: [],
    lxName: '',
    lxId: '',
    showLx: false,
    page: 0,    //页码
    is: true,   //控制是否还能切换下一页
    list: [],
  },
  calculation(s) {
    return s < 10 ? '0' + s : s
  },
  formatDate(date) {
    var date = new Date(date)
    const year = date.getFullYear() + '-'
    const month = this.calculation(date.getMonth() + 1) + '-'
    const day = this.calculation(date.getDate())
    return year + month + day
  },
  wcDate(event) {
    if (this.data.sjpd == 1) {
      if (this.data.jssj && event.detail >= this.data.currentDateb) return wx.showToast({ title: '起始时间需要小于终止日期', icon: 'none', duration: 2000 })
      this.setData({
        currentDate: event.detail,
        kssj: this.formatDate(event.detail),
        showDate: false,
        is: true,
        page: 0,
        list: []
      });
      this.getList()
    } else {
      if (this.data.kssj && event.detail <= this.data.currentDate) return wx.showToast({ title: '起始时间需要小于终止日期', icon: 'none', duration: 2000 })
      this.setData({
        currentDateb: event.detail,
        jssj: this.formatDate(event.detail),
        showDate: false,
        is: true,
        page: 0,
        list: []
      });
      this.getList()
    }
  },
  cancleDate(event) {
    this.setData({
      showDate: false
    });
  },
  dateClick() {
    this.setData({
      showDate: true,
      sjpd: 1
    });
  },
  dateClickb() {
    this.setData({
      showDate: true,
      sjpd: 2
    });
  },
  lxClick() {
    this.setData({
      showLx: true,
    });
  },
  cancleLx() {
    this.setData({
      showLx: false,
    });
  },
  onChanges(event) {
    const { value, index } = event.detail;
    console.log(event);
    this.setData({
      lxName: value.name,
      lxId: value.id,
      showLx: false,
      is: true,
      page: 0,
      list: []
    });
    this.getList()
  },
  orderTypeList() {
    orderTypeList({
      param: {
        type: "ORDER_TYPE_WX_LIST_QUERY"
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      this.setData({ columns: res.recordList })
    })
  },
  // 列表
  getList() {
    if (!this.data.is) return wx.showToast({ title: '没有更多数据了哦~', icon: 'none', duration: 2000 })
    this.setData({ page: this.data.page + 1 })
    incomeDetails({
      pagination: {
        firstQueryTime: getYMDHMS(new Date()),
        page: this.data.page,
        rows: 10
      },
      param: {
        userId: wx.getStorageSync('userId'),
        startDate: this.data.kssj,
        type: this.data.lxId,
        endDate: this.data.jssj,
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      if (res.recordList.length === 0 && this.data.page != 1 && res.recordList) {
        wx.showToast({ title: '没有更多数据了哦~', icon: 'none', duration: 2000 })
        this.setData({ is: false })
        return
      }
      if (res.recordList.length < 10 || !res.recordList) {
        this.setData({ is: false })
      }
      let arr = res.recordList
      let arrs = []
      if (arr) {
        arrs = this.data.list.concat(arr)
      } else {
        arrs = []
      }
      this.setData({ list: arrs })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.orderTypeList()
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})